package fi.profecons.csswindows;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.zip.GZIPOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

class CommandHandler {

	public void parseXML(HttpServletRequest request,
			HttpServletResponse response, WindowApplication app, State state) {
		ByteArrayOutputStream compressed = new ByteArrayOutputStream();
        
		try {
			XMLReader xr = XMLReaderFactory.createXMLReader();
			XmlHandler handler = new XmlHandler(state, app);
			xr.setContentHandler(handler);
			xr.setErrorHandler(handler);
			xr.parse(new InputSource(request.getInputStream()));
			response.setContentType("text/javascript");
			response.addHeader("Content-Encoding", "gzip");
			GZIPOutputStream out = new GZIPOutputStream(compressed);
			out.write(stateScript(state).getBytes("UTF-8"));
			out.finish();
			response.setContentLength(compressed.size());
			response.getOutputStream().write(compressed.toByteArray());
		} catch (SAXException se) {
		} catch (IOException ie) {
		}
	}

	private String stateScript(State state) {
		StringBuffer sb = new StringBuffer();
		Iterator i = state.getScript().iterator();
		while (i.hasNext()) {
			String cmd = (String) i.next();
			sb.append(cmd);
			sb.append("\r\n");
			System.out.println(cmd);
		}
		state.clearScript();
		return sb.toString();
	}

	public void sendInitialPage(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			PrintWriter out = response.getWriter();
			String theme = request.getParameter("theme");
			if (theme == null) {
				theme = "basic";
			}
			out
					.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
			out.println("<html>");
			out.println("<head>");
			out
					.println("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">");
			out.println("<title>CSSWindows</title>");
			out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"./"
					+ theme + "/styles.css\">");
			out
					.println("<script type=\"text/javascript\" src=\""
							+ request.getRequestURI()
							+ "?action=FILE_FETCH_TEXT&filename=windows.js&amp;path=/javascript/\"></script>");
			out
					.println("<script type=\"text/javascript\" src=\""
							+ request.getRequestURI()
							+ "?action=FILE_FETCH_TEXT&filename=toolkit.js&amp;path=/javascript/\"></script>");
			out
					.println("<script type=\"text/javascript\" src=\""
							+ request.getRequestURI()
							+ "?action=FILE_FETCH_TEXT&filename=actions.js&amp;path=/javascript/\"></script>");
			out
					.println("<script type=\"text/javascript\" src=\""
							+ request.getRequestURI()
							+ "?action=FILE_FETCH_TEXT&filename=Jserial.js&amp;path=/javascript/\"></script>");
			out.println("<script type=\"text/javascript\">theme = \"" + theme
					+ "\";</script>");
			out.println("</head>");
			out
					.println("<body style=\"overflow:hidden;margin:0px 0px 0px 0px;padding:0px 0px 0px 0px;\" onMouseMove=\"doDrag(event);doResize(event);\" onLoad = \"getInitialState();\"");
			out.println("</html>");
		} catch (IOException ie) {

		}
	}

}
